//
//  BTEAreaListViewController.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/18.
//

import UIKit

class BTEAreaListViewController: BTEAlertViewController {
    
    let backgroundImageView = UIImageView(image: UIImage(named: "Group efweew1171277256"))
    
    private let titleLabel = UILabel()
    
    let lists = BTEBaseTableViewController()
    
    let headerLabelContainer = UIView()
    let dayLabel = UILabel()
    let monthLabel = UILabel()
    let yearLabel = UILabel()
    
    var str: String? {
        guard let indx = a1,
              let secend = b2,
              let third = c3 else {
            return nil
        }
        // 取出对应的数据
        return  BTEAddressManager.lookingaddress(model: defaultModel!, index1: indx, index2: secend, index3: third)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

       
        
        backgroundImageView.isUserInteractionEnabled = true
        mainv.addSubview(backgroundImageView)
        backgroundImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo((UIScreen.main.bounds.width - 34) * backgroundImageView.frame.height / backgroundImageView.frame.width)
        }
        
        titleLabel.textColor = .colorWithHex("#082252")
        titleLabel.font = .systemFont(ofSize: 18, weight: .bold)
        titleLabel.textAlignment = .center
        titleLabel.text = BTELanguageManager.shared.localizedString(forKey: "Please select an address")
        mainv.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.equalTo(38)
            make.top.equalToSuperview().offset(44)
        }
        
        backgroundImageView.addSubview(headerLabelContainer)
        headerLabelContainer.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(22)
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
        }
        
        headerLabelContainer.addSubview(dayLabel)
        headerLabelContainer.addSubview(monthLabel)
        headerLabelContainer.addSubview(yearLabel)
        
        [dayLabel, monthLabel, yearLabel].forEach {
            $0.textAlignment = .center
            $0.font = .systemFont(ofSize: 16, weight: .bold)
            //$0.textColor = .colorWithHex("#999999")
        }
        dayLabel.textColor = .black
        
        dayLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(32)
        }
        
        monthLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(dayLabel.snp.bottom).offset(8)
            make.height.equalTo(32)
        }
        
        yearLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(monthLabel.snp.bottom).offset(8)
            make.height.equalTo(32)
            make.bottom.equalToSuperview()
        }
        
       
        let lineView = UIView()
        view.addSubview(lineView)
        lineView.backgroundColor = .colorWithHex("#999999", alpha: 0.3)
        lineView.snp.makeConstraints { make in
            make.left.right.equalTo(headerLabelContainer)
            make.height.equalTo(1)
            make.top.equalTo(headerLabelContainer.snp.bottom).offset(5)
        }
        
        
        backgroundImageView.addSubview(lists)
        lists.delegate = self
        lists.dataSource = self
        lists.backgroundColor = .clear
        lists.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(22)
            make.top.equalTo(headerLabelContainer.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-30)
        }
        lists.register(BTEAreaCell.self, forCellReuseIdentifier: BTEAreaCell.indetification)
        
        
        let a1 = UITapGestureRecognizer.init(target: self, action: #selector(ab1))
        let a2 = UITapGestureRecognizer.init(target: self, action: #selector(ab2))
        
        dayLabel.isUserInteractionEnabled = true
        dayLabel.addGestureRecognizer(a1)
        
        monthLabel.isUserInteractionEnabled = true
        monthLabel.addGestureRecognizer(a2)
        
        
    }
    
    var defaultModel: [BTECarneolfejubdaderesswgeNetModel]?
    
    var model:[BTECarneolfejubdaderesswgeNetModel]? {
        didSet {
            lists.reloadData()
        }
    }
    
    var callback:((String)->Void)?
    
    var a1: Int?
    
    var b2: Int?
    
    var c3: Int?
    
    init(defualt: String?,defaultModel:[BTECarneolfejubdaderesswgeNetModel],model: [BTECarneolfejubdaderesswgeNetModel]) {
        super.init(nibName: nil, bundle: nil)
        self.defaultModel = defaultModel
        if let d = defualt {
            if let t = BTEAddressManager.fewfewf222few(from: d, in: defaultModel) {
                a1 = t.index1
                b2 = t.index2
                c3 = t.index3
      
                let v = BTEAddressManager.level1fewfwbrgew(model: defaultModel, index1: t.index1, index2: t.index2,index3: t.index3)
                
                dayLabel.text = v?.level1Looking ?? BTELanguageManager.shared.localizedString(forKey: "Please choose")
                monthLabel.text = v?.level2Looking ?? BTELanguageManager.shared.localizedString(forKey: "Please choose")
                yearLabel.text = v?.level3Looking ?? BTELanguageManager.shared.localizedString(forKey: "Please choose")
                
                dayLabel.textColor = .colorWithHex("#082252")
                monthLabel.textColor = .colorWithHex("#082252")
                yearLabel.textColor = .colorWithHex("#082252")
              
                let m = defaultModel[t.index1].flinter?[t.index2].flinter
                m?.forEach({$0.choised = false})
                m?[t.index3].choised = true
                self.model = m
                
            } else {
                
                self.model = model
                dayLabel.text =  BTELanguageManager.shared.localizedString(forKey: "Please choose")
                monthLabel.text =  BTELanguageManager.shared.localizedString(forKey: "Please choose")
                yearLabel.text =  BTELanguageManager.shared.localizedString(forKey: "Please choose")
                
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}



extension BTEAreaListViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (model ?? []).count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: BTEAreaCell.indetification, for: indexPath) as? BTEAreaCell else {
            return UITableViewCell()
        }
        
        guard let ax = model?[indexPath.row] else {
            return UITableViewCell()
        }
        
        cell.subTile.text = ax.manificum
        
        if ax.choised {
            cell.subTile.textColor = .colorWithHex("#187CA5")
            cell.border.isHidden = false
        } else {
            cell.subTile.textColor = .colorWithHex("#000000")
            cell.border.isHidden = true
        }
        
        
        return cell
    }
    
    
    @objc func ab1(){
        if a1 == nil {
            return
        }
        let models = defaultModel
        self.model = models
        b2 = nil
        c3 = nil
        monthLabel.text = BTELanguageManager.shared.localizedString(forKey:"Please choose")
        yearLabel.text = BTELanguageManager.shared.localizedString(forKey:"Please choose")
      
        monthLabel.textColor = .colorWithHex("#999999")
        yearLabel.textColor = .colorWithHex("#999999")
        
        let m = model?[a1!].manificum
        dayLabel.text = m ?? ""
        
        let o = defaultModel
        o?.forEach({$0.choised = false})
        o?[a1!].choised = true
        model = o
        
        a1 = nil
    }
    
    @objc func ab2(){
        if b2 == nil {
            return
        }
        c3 = nil
        
        yearLabel.text = BTELanguageManager.shared.localizedString(forKey:"Please choose")
        
        let m = defaultModel?[a1!].flinter?[b2!].manificum
        monthLabel.text =  m ?? ""
     
        yearLabel.text = BTELanguageManager.shared.localizedString(forKey:"Please choose")
        yearLabel.textColor = .colorWithHex("#999999")
      
        
        let o = defaultModel?[a1!].flinter?[b2!].flinter
        o?.forEach({$0.choised = false})
        o?[b2!].choised = true
        model = o
    }
    
    
    
    func findeSelectedInt() -> Int {
        if  a1 == nil {
            return 0
        } else if b2 == nil {
            return 1
        } else {
            return 2
        }
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if findeSelectedInt() == 0 {
            a1 = indexPath.row
            b2 = nil
            c3 = nil
            let m = model?[indexPath.row].manificum
            dayLabel.text = m ?? ""
            dayLabel.textColor = .black
            
            monthLabel.text = BTELanguageManager.shared.localizedString(forKey:"Please choose")
            monthLabel.textColor = .colorWithHex("#999999")
            yearLabel.text = BTELanguageManager.shared.localizedString(forKey:"Please choose")
            yearLabel.textColor = .colorWithHex("#999999")
            
            let nedd = model?[indexPath.row].flinter
            nedd?.forEach({$0.choised = false})
            model = nedd
            
        } else if findeSelectedInt() == 1 {
            c3 = nil
            b2 = indexPath.row
            
            let m = model?[indexPath.row].manificum
            monthLabel.text = m ?? ""
            monthLabel.textColor = .black
            
            yearLabel.text = BTELanguageManager.shared.localizedString(forKey:"Please choose")
            yearLabel.textColor = .colorWithHex("#999999")
            
            let ne = model?[indexPath.row].flinter
            ne?.forEach({$0.choised = false})
            model = ne
            
        } else {
            c3 = indexPath.row
            let m = model?[indexPath.row].manificum
         
            yearLabel.text = m ?? ""
            yearLabel.textColor = .black
            
            model?.forEach({$0.choised = false})
            model?[indexPath.row].choised = true
            tableView.reloadData()

        }
    }
    
    
}





final class BTEAreaCell: UITableViewCell {
    
    let subTile = UILabel()
    
    let border = UIView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = .clear
        selectionStyle = .none
        contentView.backgroundColor = .clear
        
        border.isHidden = true
        contentView.addSubview(border)
        border.backgroundColor = .white
        border.roundCorners(.allCorners, radius: 16)
        border.layer.borderColor = UIColor.colorWithHex("#082252")!.cgColor
        border.layer.borderWidth = 3
        border.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.right.equalToSuperview().inset(28)
        }
        
        subTile.textAlignment = .center
        subTile.font = .systemFont(ofSize: 14, weight: .bold)
        subTile.textColor = .black
        contentView.addSubview(subTile)
        subTile.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview().inset(10)
            make.left.right.equalToSuperview().inset(18)
        }

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
